package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.domain.TravelContent;
import cn.wolfcode.luowowo.article.mapper.TravelContentMapper;
import cn.wolfcode.luowowo.article.mapper.TravelMapper;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.ITravelService;
import cn.wolfcode.luowowo.article.vo.TravelStatisPersistenceVO;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

//JPA -- spring -data-jpa
//Mybatis-plus
@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    private TravelMapper travelMapper;
    @Autowired
    private TravelContentMapper travelContentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        qo.setOrderBy("viewnum");
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        return new PageInfo(travelMapper.selectForList(qo));
    }

    @Override
    public Travel get(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public TravelContent getContent(Long id) {
        return travelContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Travel> getViewNumTop3(Long id) {
        return travelMapper.selectViewNumTop3(id);
    }

    @Override
    public Long saveOrUpdate(Travel travel) {
        //当前登录用户  在controller中设置

        //在游记对象中拿到内容对象
        String content = travel.getTravelContent().getContent();

        //当游记内容的长度超过200字时,把前面0到200字用方法substring切割出来作为摘要,长度不足时把所有内容都设置进去
        if (content.length()>=200){
            travel.setSummary(content.substring(0,200));

        }else {
            travel.setSummary(content);
        }

        //设置最后更新时间为当前新增或修改时间
        travel.setLastUpdateTime(new Date());

        //根据当前游记是否有id判断并进行新增或修改
        if (travel.getId() == null){
            //设置创建时间
            travel.setCreateTime(new Date());
            travelMapper.insert(travel);
            //因为游记和内容是分开管理的,所以同时也要把内容设置进去
            //同时不要忘记设置id,游记和内容id是一一对应的
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.insert(travelContent);
        }else {
            travelMapper.updateByPrimaryKey(travel);
            //同理可得
            TravelContent travelContent = travel.getTravelContent();
            travelContent.setId(travel.getId());
            travelContentMapper.updateByPrimaryKey(travelContent);
        }

        //该方法需要返回一个id给页面进行跳转显示整个游记内容
        return travel.getId();
    }

    @Override
    public List<Travel> list() {
        return travelMapper.selectAll();
    }

    @Override
    public void updateStatisVO(TravelStatisPersistenceVO vo) {
        travelMapper.updateStatisVO(vo);
    }
}
